/*
 * MemoryProcessingInstruction.java
 *
 * Created on August 3, 2004, 8:10 AM
 */

package org.infoset.xml.memory;

import java.util.*;
import org.infoset.xml.*;
import org.infoset.xml.util.*;

/**
 *
 * @author  R. Alexander Milowski
 */
public class MemoryProcessingInstruction extends MemoryTextDataItem implements ProcessingInstruction {

   int index;
   int line;
   int column;
   Name name;
   /** Creates a new instance of MemoryProcessingInstruction */
   public MemoryProcessingInstruction(Infoset infoset,Parent origin, Name name,String textValue) {
      super(infoset,origin);
      setText(textValue);
      this.line = line;
      this.column = column;
      this.name = name;
      if (name.getNamespaceName()!=Name.NO_NAMESPACE) {
         throw new IllegalArgumentException("Processing instructions cannot have namespaces.");
      }
      if (name.getLocalName().equalsIgnoreCase("xml")) {
         throw new IllegalArgumentException("Processing instructions cannot have target names of [xX][mM][lL].");
      }
   }
   
   public Item copyOfItem(boolean copyAll) {
      MemoryProcessingInstruction pi = new MemoryProcessingInstruction(infoset,null,name,value);
      pi.line = line;
      pi.column = column;
      return pi;
   }
   
   public ItemType getType() {
      return ItemType.ProcessingInstructionItem;
   }
   
   protected void setIndex(int i) {
      this.index = i;
   }
   
   public int getColumn() {
      return column;
   }   

   public int getLine() {
      return line;
   }
   
   public void setColumn(int column) {
      this.column = column;
   }
   
   public void setLine(int line) {
      this.line = line;
   }
   
   public Name getName() {
      return name;
   }
   
   public void setName(Name name) {
      if (name.getNamespaceName()!=null) {
         throw new IllegalArgumentException("Processing instructions cannot have namespaces.");
      }
      this.name = name;
   }
   
   public void setName(String localName) {
      this.name = infoset.createName(localName);
   }
   
   public void setName(java.net.URI namespaceName, String localName) {
      if (namespaceName!=null) {
         throw new IllegalArgumentException("Processing instructions cannot have namespaces.");
      }
      this.name = infoset.createName(namespaceName,localName);
   }
   
   public Parent getParent() {
      return getOrigin();
   }
   
   public Child getFollowingSibling() {
      return (Child)origin.get(index+1);
   }

   public Child getPrecedingSibling() {
      return (Child)origin.get(index-1);
   }
   
   public Iterator getFollowingSiblings() {
      return new SiblingIterator(getFollowingSibling(),true);
   }
   
   public Iterator getPrecedingSiblings() {
      return new SiblingIterator(getPrecedingSibling(),false);
   }
   
   public Object clone() {
      MemoryProcessingInstruction child = new MemoryProcessingInstruction(infoset,getParent(), name,value);
      child.setIndex(index);
      return child;
   }
}
